<?php

class Categories extends La_Controller {
	function __construct() {
		parent::__construct();
		$this->load->model("Category_model");
		if (!$this->authorize->is_admin_logged_in()) {
			la_redirect("/admin/login", "请先登录");
		}
		$this->data['nav'] = 'category';
	}
	function index() {
		$category_id = $this->input->get("category_id");
		if (empty($category_id)) {
			$category_id = 1;
		}
		$categories = $this->Category_model->find_son_category($category_id);
		$this->data['categories'] = $categories;
		$this->data['category_id'] = $category_id;
		$this->load->view('admin/categories', $this->data);
	}	
	/**
	 * 添加分类
	 */
	function add_son() {
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$category_id = $this->input->post("id");
			$name = $this->input->post("name");
			$this->Category_model->insert_child_category( $name, $category_id);
			redirect("/console/categories");
		} else {
			$category_id = $this->input->get("category_id");
			if (!$category_id) {
				la_redirect("/console/categories", "错误的参数");
			}
			$father_category = $this->Category_model->find_category_by_id($category_id);
			$this->data['category_id'] = $category_id;
			$this->data['father_category'] = $father_category;
			$this->load->view('admin/edit_category', $this->data);
		}
	}
	function delete() {
		$category_id = $this->input->get("category_id");
		$this->Category_model->delete_category_with_child($category_id);
		redirect("/console/categories");
	}
	/**
	 * 添加兄弟分类
	 */
	function add_sibling() {
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$category_id = $this->input->post("id");
			$name = $this->input->post("name");
			$this->Category_model->insert_sibling_category( $name, $category_id);
			redirect("/console/categories");
		} else {
			$category_id = $this->input->get("category_id");
			if (!$category_id) {
				la_redirect("/console/categories", "错误的参数");
			}
			$sibling_category = $this->Category_model->find_category_by_id($category_id);
			$this->data['category_id'] = $category_id;
			$this->data['sibling_category'] = $sibling_category;
			$this->load->view('admin/edit_category', $this->data);
		}
	}
	/**
	 * 编辑分类
	 */
	function edit() {
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$category_id = $this->input->post("id");
			$name = $this->input->post("name");
			$ordering = $this->input->post("ordering");
			$this->Category_model->update_category($category_id, array('name' => $name, 'ordering' => $ordering));
			redirect("/console/categories");
		} else {
			$category_id = $this->input->get("category_id");
			if (!$category_id) {
				la_redirect("/console/categories", "错误的参数");
			}
			$category = $this->Category_model->find_category_by_id($category_id);
			$this->data['category_id'] = $category_id;
			$this->data['category'] = $category;
			$this->load->view('admin/edit_category', $this->data);
		}
	}
	
	function save_ordering() {
		$ordering = $this->input->post("ordering");
		$category_id = $this->input->post("categroy_id");
		foreach($ordering as $key => $value) {
			$this->Category_model->set_ordering($key, $value);
		}
		la_redirect("/console/categories?category_id=" . $category_id, "修改成功");
	}
}